package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.kimkevin.cachepot.CachePot;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.CircleArticleItem;
import ahblue.cn.tanzi.model.circle.CircleDynamicItem;
import ahblue.cn.tanzi.model.home.ArticlesForCategory;
import ahblue.cn.tanzi.ui.home.article.ArticleDetailActivity;
import ahblue.cn.tanzi.ui.home.article.ArticleListFragment;
import ahblue.cn.tanzi.util.GlideApp;
import ahblue.cn.tanzi.util.TimeUtils;
import ahblue.cn.tanzi.view.MyLinearLayoutManager;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * @author rhino
 * @since Create on 2019/3/1.
 **/
public class CircleArticleListFragment extends Fragment {

    @BindView(R.id.recyclerViewArticleList)
    RecyclerView mRecyclerView;
    @BindView(R.id.wv_circle_introduce)
    WebView wvCircleIntroduce;

    private List<CircleArticleItem> mArticleList = new ArrayList<>();
    private ArticlesForCategoryAdapter mArticlesAdapter;

    private final static int ARG_PAGE_COUNT = 10;
    private int mPage = 0;
    private int mCircleId;

    private MyLinearLayoutManager myLinearLayoutManager;

    public static CircleArticleListFragment newInstance(int mCircleId) {
        CircleArticleListFragment fragment = new CircleArticleListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("CircleId", mCircleId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mCircleId = getArguments().getInt("CircleId");
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_article_list, container, false);
        ButterKnife.bind(this, view);
        wvCircleIntroduce.setVisibility(View.GONE);
        mArticlesAdapter = new ArticlesForCategoryAdapter(mArticleList);
        mArticlesAdapter.setOnItemClickListener((adapter, v, position) -> {
            CircleArticleItem item = mArticleList.get(position);
            final Intent intent = new Intent(getContext(), ArticleDetailActivity.class);
            intent.putExtra(ArticleDetailActivity.ARG_ARTICLE_ID, item.id);
            intent.putExtra(ArticleDetailActivity.ARG_ARTICLE_TITLE, item.content);
            startActivity(intent);
        });
        mRecyclerView.setAdapter(mArticlesAdapter);

        myLinearLayoutManager = new MyLinearLayoutManager(getContext());
        myLinearLayoutManager.setScrollEnabled(false);
        mRecyclerView.setLayoutManager(myLinearLayoutManager);

        mRecyclerView.setFocusableInTouchMode(false);
        mRecyclerView.requestFocus();

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        RemoteRepository repo = RemoteRepository.getsInstance();
        Disposable d = repo.getCircleDetailsArticle(mCircleId, mPage, ARG_PAGE_COUNT, mCallback);
    }

    private ApiCallback<List<CircleArticleItem>> mCallback = new ApiCallback<List<CircleArticleItem>>() {
        @Override
        public void onSuccess(List<CircleArticleItem> data) {
            mArticleList.clear();
            if (data != null) {
                mArticleList.addAll(data);
            }
            mArticlesAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    /**
     * 文章列表的adapter
     */
    class ArticlesForCategoryAdapter extends BaseQuickAdapter<CircleArticleItem, BaseViewHolder> {

        ArticlesForCategoryAdapter(@Nullable List<CircleArticleItem> data) {
            super(R.layout.item_home_article, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CircleArticleItem item) {
            ImageView iv = helper.getView(R.id.iv_news_cover);
            GlideApp.with(iv).load(item.image)
                    .placeholder(R.drawable.user_default_avatar)
                    .error(R.drawable.user_default_avatar)
                    .into(iv);
            helper.setText(R.id.tv_news_title, item.content);
            helper.setText(R.id.tv_news_author, item.member_name);
            helper.setText(R.id.tv_news_time, TimeUtils.getStandardDate(item.add_time));
            helper.setText(R.id.tv_read_count, "阅读 " + Integer.toString(0));

            helper.setVisible(R.id.tv_read_count, false);
        }
    }

    public void setCanScrollable(boolean canScrollable){
        myLinearLayoutManager.setScrollEnabled(canScrollable);
    }
}
